package com.ipartek.formacion.dado.ddbb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.ipartek.formacion.dado.dao.MySqlAlumnoDAO;
import com.ipartek.formacion.dado.dao.MySqlTiradaDAO;
import com.ipartek.formacion.dado.interfaces.InterfaceAlumnoDAO;
import com.ipartek.formacion.dado.interfaces.InterfaceTiradaDAO;

/**
 * 
 * @author Fila3+1
 */
public class MySqlDAOFactory extends DAOFactory {
	// inicializo un DataSource (para utilizar el context.xml
	// Parametros configuracion base de datos
	private static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver";
	private static final String DB_NAME = "dado";
	private static final String DB_PORT = "3306";
	private static final String DB_USER = "root";
	private static final String DB_PASS = "curso";
	private static final String DB_SERVER = "localhost";
	private static final String DB_URL = "jdbc:mysql://" + DB_SERVER + ":"
			+ DB_PORT + "/" + DB_NAME;

	private static final Logger log = Logger.getLogger(MySqlDAOFactory.class);

	// Constructor
	// ************************************************************************
	public MySqlDAOFactory() {
		PropertyConfigurator.configure("config/log4j.properties");
	}

	// Metodos
	// ************************************************************************

	// Conectar con la base de datos
	public static Connection conectar() throws Exception {
		Connection conexion = null; // Creamos un tipo connection
		try {
			Class.forName(DRIVER_MYSQL);

			// Define URL of database server for
			// database named mysql on the localhost
			// with the default port number 3306.
			// Get a connection to the database for a
			// user named root with a blank password.
			// This user is the default administrator
			// having full privileges to do anything.

			log.trace("Conectamos con la ddbb");

			conexion = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
			conexion.setAutoCommit(true);

			log.trace("Conectados con la ddbb " + DB_URL);
		} catch (ClassNotFoundException e) {
			log.error("ClassNotFoundException driver no encontrado "
					+ DRIVER_MYSQL);
		} catch (SQLException e) {
			while (e != null) {
				log.error("Message: " + e.getMessage() + "SQLState: "
						+ e.getSQLState() + "ErrorCode: " + e.getErrorCode());
				e = e.getNextException();
			}
		} catch (Exception e) {
			log.error("Exception general al conectar con la ddbb "
					+ e.getMessage());
		}
		log.trace("Devolvemos la conexion con la ddbb");
		return conexion;
	}

	// Desconectar con la base de datos
	public static void desconectar(Connection conexion) {
		if (conexion != null) {
			try {
				conexion.close();
			} catch (SQLException ex) {
				log.error("Exception general al desconectar con la ddbb "
						+ ex.getMessage());
			}
		}
	}

	// Metodos que tengo que implementar al extender de DAOFActory
	// *************************************************************************
	@Override
	public InterfaceAlumnoDAO getInterfaceAlumnoDAO() {
		return new MySqlAlumnoDAO("alumno", "tirada");
	}

	@Override
	public InterfaceTiradaDAO getInterfaceTiradaDAO() {
		return new MySqlTiradaDAO("alumno", "tirada");
	}

}
